/**************************************************************
 *@ProjectName: blweibo
 *@FileName: AccountHelper.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.zhiweibo.models.Account;
import com.zhiweibo.models.StaticInfo;

/**
 * The Class AccountHelper.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class AccountHelper {
    
    /** The Constant PREFS_NICK_NAME. */
    public static final String PREFS_NICK_NAME = "nickname";
    
    /** The Constant PREFS_PASS_WORD. */
    public static final String PREFS_PASS_WORD = "password";
    
    /** The Constant PREFS_USER_FILE. */
    public static final String PREFS_USER_FILE = "accounts";
    
    /** The Constant PREFS_USER_NAME. */
    public static final String PREFS_USER_NAME = "username";

    /**
     * Adds the or sort account.
     * 
     * @param paramList
     *            the param list
     * @param paramAccount
     *            the param account
     */
    public static void addOrSortAccount(List<Account> paramList, Account paramAccount) {
        Iterator localIterator = paramList.iterator();
        while (localIterator.hasNext()) {
            Account account2 = (Account) localIterator.next();
            if (account2.mUid.equals(paramAccount.mUid)) {
                paramList.remove(paramAccount);
                break;
            }
        }
        paramList.add(0, paramAccount);
    }

    /**
     * Adds the.
     * 
     * @param paramContext
     *            the param context
     */
    public static void add(Context paramContext) {
        List paramList = loadAccounts(paramContext);
        Iterator localIterator = paramList.iterator();
        while (localIterator.hasNext()) {
            Account account2 = (Account) localIterator.next();
            if (account2.mUid.equals(StaticInfo.mUid)) {
                return;
            }
        }
        Account ac = new Account();
        ac.setmNickName(StaticInfo.mNickName);
        ac.setmPassword(StaticInfo.mPassword);
        ac.setmUid(StaticInfo.mUid);
        ac.setmUsername(StaticInfo.mUsername);
        paramList.add(0, ac);
        saveAccounts(paramContext, paramList);
    }

    /**
     * Clean accounts.
     * 
     * @param paramContext
     *            the param context
     */
    public static void cleanAccounts(Context paramContext) {
        SharedPreferences.Editor localEditor = paramContext.getSharedPreferences("accounts", 0).edit();
        localEditor.clear();
        localEditor.commit();
    }

    /**
     * Find account.
     * 
     * @param paramList
     *            the param list
     * @param paramString
     *            the param string
     * @return the account
     */
    public static Account findAccount(List<Account> paramList, String paramString) {
        for (int i = 0; i < paramList.size(); i++) {
            if (paramList.get(i).mUsername.equals(paramString)) {
                return paramList.get(i);
            }
        }
        return null;
    }

    /**
     * Load accounts.
     * 
     * @param paramContext
     *            the param context
     * @return the list
     */
    public static List loadAccounts(Context paramContext) {
        String str1 = "";
        SharedPreferences localSharedPreferences = paramContext.getSharedPreferences("accounts", 0);
        ArrayList localArrayList = new ArrayList();
        for (int i = 0; i < 5; i++) {
            String str3 = localSharedPreferences.getString("username" + i, str1);
            String str5 = localSharedPreferences.getString("password" + i, str1);
            String str7 = localSharedPreferences.getString("nickname" + i, str1);
            String str9 = localSharedPreferences.getString("uid" + i, str1);
            if (str3.length() > 0) {
                Account localAccount = new Account();
                localAccount.mUsername = str3;
                localAccount.mPassword = str5;
                localAccount.mNickName = str7;
                localAccount.mUid = str9;
                localArrayList.add(localAccount);
            }
        }
        return localArrayList;
    }

    /**
     * Save accounts.
     * 
     * @param paramContext
     *            the param context
     * @param paramList
     *            the param list
     */
    public static void saveAccounts(Context paramContext, List paramList) {
        Editor localEditor = paramContext.getSharedPreferences("accounts", 0).edit();
        localEditor.clear();
        for (int i = 0; i < paramList.size(); i++) {
            localEditor.putString("username" + i, ((Account) paramList.get(i)).mUsername);
            localEditor.putString("password" + i, ((Account) paramList.get(i)).mPassword);
            localEditor.putString("nickname" + i, ((Account) paramList.get(i)).mNickName);
            localEditor.putString("uid" + i, ((Account) paramList.get(i)).mUid);
        }
        localEditor.commit();
    }
}